package com.ubeecube.mylocker.utility;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

public class Utility {

	
	/**
	 * Returns the file path from the given Uri
	 * @param contentUri
	 * @return
	 */
	public static String getRealPathFromURI(Uri contentUri, Activity activity, int fileType) { 
		String filePath = null;
		Cursor cursor = null;
		int column_index;
		// can post image 
		if (fileType == Constants.PICTURES) {
			
			String[] proj = {MediaStore.Images.Media.DATA}; 
			cursor = activity.managedQuery( contentUri, 
					proj, // Which columns to return 
					null, // WHERE clause; which rows to return (all rows) 
					null, // WHERE clause selection arguments (none) 
					null); // Order-by clause (ascending by name) 
			column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
			cursor.moveToFirst(); 
			filePath = cursor.getString(column_index);
			
		} else if (fileType == Constants.VIDEOS) {
			
			String[] proj = {MediaStore.Video.Media.DATA}; 
			cursor = activity.managedQuery( contentUri, 
					proj, // Which columns to return 
					null, // WHERE clause; which rows to return (all rows) 
					null, // WHERE clause selection arguments (none) 
					null); // Order-by clause (ascending by name) 
			column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA); 
			cursor.moveToFirst(); 
			filePath = cursor.getString(column_index);
			
		} else if (fileType == Constants.FILES) {
			filePath = contentUri.getPath();
		}
		 
		
		return filePath;
	}
	
	
	public static String checkcreateFolder(){
		File f = new File(Constants.APP_ROOT_PATH);
        if (!f.exists())
        {
            f.mkdirs();
            
        }
        return f.getAbsolutePath();
	}
	
	public static void copyFile(File sourceFile, File destFile) throws IOException {
		if (!sourceFile.exists()) {
			return;
		}
		FileChannel source = null;
		FileChannel destination = null;
		destFile.createNewFile();
		source = new FileInputStream(sourceFile).getChannel();
		destination = new FileOutputStream(destFile).getChannel();
		if (destination != null && source != null) {
			destination.transferFrom(source, 0, source.size());
		}
		if (source != null) {
			source.close();
		}
		if (destination != null) {
			destination.close();
		}
}
	
	public static void renameFile(File f, String fileType){
        if (!f.exists())
        {
        	return;
        }
        f.renameTo(new File(f.getAbsolutePath() + fileType));
	}
	
	public static void deleteFile(File f){
        if (!f.exists())
        {
        	return;
        }
        f.delete();
	}
	
	public static byte[] getBytesFromFile(InputStream is)
	{
		try
		{
			ByteArrayOutputStream buffer = new ByteArrayOutputStream();

			int nRead;
			byte[] data = new byte[16384];

			while ((nRead = is.read(data, 0, data.length)) != -1)
			{
				buffer.write(data, 0, nRead);
			}

			buffer.flush();

			return buffer.toByteArray();
		} catch (IOException e)
		{
			Log.e("com.eggie5.post_to_eggie5", e.toString());
			return null;
		}
	}
}
